package com.koron.order.overhaul.bean.entity;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.koron.bean.annocation.FillField;
import com.koron.order.common.bean.po.OrderLocationPo;
import com.koron.order.common.bean.po.OrderWorkDeptPo;
import com.koron.order.overhaul.utils.Constants;
import com.koron.order.overhaul.utils.DeleteFlag;
import com.koron.order.overhaul.utils.UpdateParameter;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (OverhaulOrderBean) 大修工单主实体
 *
 * @author twcao
 * @since 2021-09-29 09:49:26
 */
@Data
@ApiModel("大修工单-基础信息")
public class OverhaulOrderBean implements UpdateParameter, Serializable, DeleteFlag {

    private static final long serialVersionUID = -45296635304380867L;

    @ApiModelProperty(value = "ID,32位随机数，主键")
    private String id;

    @ApiModelProperty(value = "大修工单编码")
    private String code;

    @ApiModelProperty(value = "大修工单标题")
    private String title;

    @ApiModelProperty(value = "设备管理单位ID")
    private String deptManage;

    @ApiModelProperty(value = "设备管理单位名称")
    private String deptManageName;

    @ApiModelProperty(value = "设备ID")
    private String equipmentId;

    @ApiModelProperty(value = "设备名称")
    private String equipmentName;

    @ApiModelProperty(value = "专业")
    private String major;

    @ApiModelProperty(value = "工单性质")
    private Boolean nature;

    @ApiModelProperty(value = "工单来源")
    private String source;

    @ApiModelProperty(value = "工单来源的ID")
    private String sourceId;

    @ApiModelProperty(value = "主办单位")
    private String deptWork;

    @ApiModelProperty(value = "主办单位名称")
    private String deptWorkName;

    @ApiModelProperty(value = "工作地点ID")
    private String location;

    @ApiModelProperty(value = "工作地点名称")
    private String locationName;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm", timezone = "GMT+8")
    @ApiModelProperty(value = "预计开始时间")
    private Date startTime;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm", timezone = "GMT+8")
    @ApiModelProperty(value = "预计结束时间")
    private Date endTime;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty(value = "实际结束时间")
    private Date factEndTime;

    @ApiModelProperty(value = "工作内容")
    private String context;

    @ApiModelProperty(value = "备注")
    private String marks;

    @ApiModelProperty(value = "工单状态")
    private String status;

    @ApiModelProperty(value = "创建单位ID")
    @FillField(alias = "orgId")
    private String deptId;

    @ApiModelProperty(value = "创建单位名称")
    @FillField(alias = "orgName")
    private String deptName;

    @ApiModelProperty(value = "创建人账号")
    private String createBy;

    @ApiModelProperty(value = "创建人姓名")
    private String createByName;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty(value = "创建时间")
    private Date createTime;

    @FillField
    @ApiModelProperty(value = "更新人账号")
    private String updateBy;

    @FillField
    @ApiModelProperty(value = "更新人姓名")
    private String updateByName;

    @FillField
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty(value = "更新时间")
    private Date updateTime;

    @JsonIgnore
    @ApiModelProperty(value = "删除标志,0：delete_flag=false，代表未删除 1：delete_flag = true，代表已删除", hidden = true)
    private Boolean deleteFlag;

    @ApiModelProperty("状态红绿灯")
    private String signal;

    @ApiModelProperty(value = "工单工作地点信息")
    private List<OrderLocationPo> orderLocationList;

    @ApiModelProperty(value = "工单作业单位信息")
    private List<OrderWorkDeptPo> deptWorkList;

    @ApiModelProperty(value = "计划id")
    private String planId;

    @ApiModelProperty(value = "计划code")
    private String planCode;

    /**
     * 检修等级：1：A修；2：B修
     */
    @ApiModelProperty(value = "检修等级; 1：A修；2：B修")
    private Integer repairLevel;

    /**
     * 1：计划大修；2：故障大修；3：改造
     */
    @ApiModelProperty(value = "检修类别; 1：计划大修；2：故障大修；3：改造")
    private Integer repairType;

    @ApiModelProperty(value = "施工单位")
    private String buildUnit;

    @ApiModelProperty(value = "提交时间：用于记录执行中到待提交分析报告的时间")
    private Date submitTime;

    @ApiModelProperty(value = "超期通知标识：执行中提交到分析报告的时间超过15天")
    private boolean overTime;

    public List<OrderLocationPo> getOrderLocationList() {
        if (CollectionUtils.isEmpty(orderLocationList)) {
            return new ArrayList<>();
        }
        return orderLocationList;
    }

    public String getSignal() {
        if (Constants.OverhaulStatus.ARCHIVED.equals(this.getStatus())) {
            // 已完成(如已经完成，则表明更新时间为计划完成时间)
            if ((getUpdateTime().compareTo(getEndTime()) <= 0)) {
                // 完成时间 <= 预计结束时间
                return Constants.Signal.ON_TIME;
            } else {
                // 完成时间 > 预计结束时间
                return Constants.Signal.EXPIRED;
            }
        } else {
            // 未完成
            if (getEndTime() == null || (getEndTime().getTime() - System.currentTimeMillis()) > Constants.EXPIRING_DURATION) {
                // 距离计划结束超过3天时间
                return Constants.Signal.ON_TIME;
            } else if ((getEndTime().getTime() - System.currentTimeMillis()) > 0 && (getEndTime().getTime() - System.currentTimeMillis()) <= Constants.EXPIRING_DURATION) {
                // 0  <  预计完成时间 - 当前时间  < 3天   ===> 即还剩不到3天就到计划完成时间了
                return Constants.Signal.EXPIRING;
            } else {
                // 预计计划结束时间已经超过3天了
                return Constants.Signal.EXPIRED;
            }
        }
    }

    public boolean isOverTime() {
        if (getSubmitTime() != null && getStatus().equals(Constants.OverhaulStatus.PREPARE_REPORT)){
           if (DateUtil.between(getSubmitTime(),new Date(), DateUnit.DAY) >= 15){
               return true;
           }
        }
        return false;
    }

}
